package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;


    /**
     * 保存品牌信息
     * @param brandDTO  品牌dto
     * @param cids      分类id集合
     * @return
     */
    @PostMapping
    public  ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids){
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *  分页条件查询
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value="key",required = false)  String key, // 查询条件
            @RequestParam(value="page",required = false,defaultValue = "1")  Integer page, // 当前页码
            @RequestParam(value="rows",required = false,defaultValue = "10")  Integer rows, // 每页显示条数
            @RequestParam(value="sortBy",required = false,defaultValue = "id")  String sortBy, //排序的字段
            @RequestParam(value="desc",required = false,defaultValue = "false")  Boolean desc//  是否排序

    ){
        // 调用service去查询
        PageResult<BrandDTO> pageResult = brandService.queryBrandByPage(key, page, rows, sortBy, desc);
        // 返回
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 根据分类id查询品牌列表
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(cid));
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id ){
        return ResponseEntity.ok(brandService.queryBrandById(id));
    }

    // 批量查询品牌
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }
}
